from libcpp cimport nullptr_t, nullptr
from cpython.ref cimport PyObject
from cython cimport bint
from libc.stdlib cimport malloc, free
from libc.time cimport tm

cdef extern from 'Python.h':
    ctypedef enum PyGILState_STATE:
        pass
    ctypedef struct PyThreadState:
        pass

    PyGILState_STATE PyGILState_Ensure()
    void PyGILState_Release(PyGILState_STATE)

    PyThreadState * PyEval_SaveThread()
    void PyEval_RestoreThread(PyThreadState *)

    void PyEval_InitThreads()

    void Py_INCREF(object obj)
    void Py_DECREF(object obj)
    void Py_XINCREF(object obj)
    void Py_XDECREF(object obj)

cdef extern from 'stdafx.h':
    ctypedef PyObject * PyObject_Point
    ctypedef Py_UNICODE wchar_t
    ctypedef wchar_t WCHAR
    ctypedef char CHAR
    ctypedef WCHAR TCHAR
    ctypedef TCHAR *LPTSTR
    ctypedef const TCHAR * LPCTSTR
    ctypedef WCHAR *LPWSTR
    ctypedef const WCHAR * LPCWSTR
    ctypedef char *LPSTR
    ctypedef const char * LPCSTR
    ctypedef void* LPVOID
    ctypedef void* HINSTANCE
    ctypedef HINSTANCE HMODULE
    ctypedef void* HBITMAP
    ctypedef void* HWND
    ctypedef void* HICON
    ctypedef void* HMENU
    ctypedef void* HDC
    ctypedef void* HRGN
    ctypedef void* HCURSOR
    ctypedef int BOOL
    ctypedef int UINT
    ctypedef int *LPINT
    ctypedef bint bool
    ctypedef unsigned int INT_PTR
    ctypedef unsigned int UINT_PTR
    ctypedef UINT_PTR WPARAM
    ctypedef long LPARAM
    ctypedef long LONG
    ctypedef unsigned long DWORD
    ctypedef unsigned long ULONG_PTR
    ctypedef unsigned long DWORD_PTR
    ctypedef long long int64_t
    ctypedef unsigned long long uint64_t
    ctypedef long LONG_PTR
    ctypedef LONG_PTR LRESULT
    ctypedef long HRESULT
    ctypedef short WORD
    ctypedef unsigned char BYTE
    ctypedef BYTE* LPBYTE
    ctypedef DWORD COLORREF

    nullptr_t NULL

    ctypedef ULONG_PTR HSTREEITEM
    ctypedef ULONG_PTR HTREEITEM 'SOUI::HTREEITEM'

    ctypedef struct tagPOINT:
        LONG x
        LONG y
    ctypedef tagPOINT POINT
    ctypedef tagPOINT* LPPOINT
    ctypedef const LPPOINT LPCPOINT

    ctypedef struct tagSIZE:
        LONG cx
        LONG cy
    ctypedef tagSIZE SIZE
    ctypedef tagSIZE *PSIZE
    ctypedef PSIZE LPSIZE

    ctypedef struct tagRECT:
        LONG    left
        LONG    top
        LONG    right
        LONG    bottom
    ctypedef tagRECT RECT
    ctypedef tagRECT *PRECT
    ctypedef const PRECT LPCRECT
    ctypedef PRECT NPRECT
    ctypedef PRECT LPRECT

    ctypedef struct _iobuf:
        void* _Placeholder
    ctypedef _iobuf FILE

cdef extern from 'stdafx.h':
    ctypedef enum BUILTIN_RESTYPE:
        RES_PE = 0
        RES_FILE = 1

    ctypedef enum BlurStyle:
        kNone_BLurStyle = -1
        kNormal_BlurStyle = 0
        kSolid_BlurStyle = 1
        kOuter_BlurStyle = 2
        kInner_BlurStyle = 3

        kLastEnum_BlurStyle = 3

    ctypedef enum BlurFlags:
        kNone_BlurFlag = 0x00
        kIgnoreTransform_BlurFlag = 0x01
        kHighQuality_BlurFlag = 0x02
        kAll_BlurFlag = 0x03

    ctypedef enum FilterLevel:
        kNone_FilterLevel = 0
        kLow_FilterLevel = 1
        kMedium_FilterLevel = 2
        kHigh_FilterLevel = 3

    ctypedef struct tagMSG:
        HWND        hwnd;
        UINT        message;
        WPARAM      wParam;
        LPARAM      lParam;
        DWORD       time;
        POINT       pt;
    ctypedef tagMSG MSG
    ctypedef MSG *PMSG
    ctypedef PMSG LPMSG
    ctypedef PMSG NPMSG

    ctypedef DWORD SWND

    ctypedef enum OBJTYPE:
        OT_NULL = 0
        OT_PEN = 1
        OT_BRUSH = 2
        OT_BITMAP = 3
        OT_FONT = 4
        OT_RGN = 5
        OT_PATH = 6

    ctypedef enum SingletonType:
        SINGLETON_UIDEF = 0
        SINGLETON_SWNDMGR = 1
        SINGLETON_TIMERGENERATOR = 2
        SINGLETON_WINDOWFINDER=3
        SINGLETON_RICHEDITMENUDEF=4
        SINGLETON_HOSTMGR=5
        SINGLETON_NOTIFYCENTER=6
        SINGLETON_COUNT=7

    ctypedef enum SObjectType:
        Undef = 0
        Window = 1
        Skin = 2
        Layout = 3
        LayoutParam = 4
        Event = 5
        Interpolator = 6
        Animation = 7
        ValueAnimator = 8
        UserType = 9

    ctypedef enum Priority:
        High = 1
        Normal = 0
        Low = -1

    ctypedef enum RepeatMode:
        RESTART = 1
        REVERSE = 2
    
    ctypedef enum ZAdjustment:
        ZORDER_NORMAL = 0
        ZORDER_TOP = 1
        ZORDER_BOTTOM = -1

    ctypedef enum GrtFlag:
        GRT_NODRAW = 0
        GRT_PAINTBKGND = 1
        GRT_OFFSCREEN = 2

    ctypedef enum GradientType:
        linear = 0 # /*<线性渐变*/
        radial = 1 # /*<辐射渐变*/
        sweep = 2  # /*<扫描渐变*/

    ctypedef enum TileMode:
        kClamp_TileMode = 0
        kRepeat_TileMode = 1
        kMirror_TileMode = 2

    ctypedef enum FillType:
        kWinding_FillType = 0
        kEvenOdd_FillType = 1
        kInverseWinding_FillType = 2
        kInverseEvenOdd_FillType = 3

    ctypedef enum Convexity:
        kUnknown_Convexity = 0
        kConvex_Convexity = 1
        kConcave_Convexity = 2

    ctypedef enum XmlEncoding:
        enc_auto = 0		# // Auto-detect input encoding using BOM or < / <? detection; use UTF8 if BOM is not found
        enc_utf8 = 1		# // UTF8 encoding
        enc_utf16_le = 2	# // Little-endian UTF16
        enc_utf16_be = 3	# // Big-endian UTF16
        enc_utf16 = 4		# // UTF16 with native endianness
        enc_utf32_le = 5	# // Little-endian UTF32
        enc_utf32_be = 6	# // Big-endian UTF32
        enc_utf32 = 7		# // UTF32 with native endianness
        enc_wchar = 8		# // The same encoding wchar_t has (either UTF16 or UTF32)
        enc_latin1 = 9
        enc_bin = 10		# // bin xml

    ctypedef enum XmlStatus:
        xml_ok = 0				    # // No error
        xml_file_not_found = 1		# // File was not found during load_file()
        xml_io_error = 2			# // Error reading from file/stream
        xml_out_of_memory = 3		# // Could not allocate memory
        xml_internal_error = 4		# // Internal error occurred
        xml_unrecognized_tag = 5	# // Parser could not determine tag type
        xml_bad_pi = 6				# // Parsing error occurred while parsing document declaration/processing instruction
        xml_bad_comment = 7		    # 	// Parsing error occurred while parsing comment
        xml_bad_cdata = 8			# // Parsing error occurred while parsing CDATA section
        xml_bad_doctype = 9		    # 	// Parsing error occurred while parsing document type declaration
        xml_bad_pcdata = 10			# // Parsing error occurred while parsing PCDATA section
        xml_bad_start_element = 11	# // Parsing error occurred while parsing start element tag
        xml_bad_attribute = 12		# // Parsing error occurred while parsing element attribute
        xml_bad_end_element = 13		# // Parsing error occurred while parsing end element tag
        xml_end_element_mismatch = 14   # // There was a mismatch of start-end tags (closing tag had incorrect name, some tag was not closed or there was an excessive closing tag)
        xml_append_invalid_root = 15	# // Unable to append nodes since root type is not node_element or node_document (exclusive to xml_node::append_buffer)
        xml_no_document_element = 16	# // Parsing resulted in a document without element nodes

    ctypedef int ptrdiff_t

    ctypedef struct XmlParseResult:
        XmlStatus status
        ptrdiff_t offset
        XmlEncoding encoding

    ctypedef struct tagGUID:
        unsigned long  Data1
        unsigned short Data2
        unsigned short Data3
        unsigned char  Data4[8]
    ctypedef tagGUID GUID
    ctypedef const GUID *const REFGUID

    ctypedef struct TIPID:
        DWORD dwHi  # ID1, 用来保存一个SWND。
        DWORD dwLow # ID2, 用来保存附加数据
        BOOL bNcTip # 非客户区tip

    ctypedef struct LOGFONTW:
        pass
    ctypedef LOGFONTW LOGFONT


    ctypedef BOOL(__stdcall *EnumResCallback)(LPCTSTR pszType, LPCTSTR pszName, LPARAM lp);

    ctypedef void(*FunTimerCallBack)(HWND, UINT, UINT_PTR, DWORD);

    ctypedef void (*FunRunOnUI)(WPARAM wp, LPARAM lp);

cdef extern from '<stdio.h>' nogil:
    wchar_t* wcscpy(wchar_t* dst, const wchar_t* src)